<?php
    if(isset($_GET['id']))
    {
        $id = $_GET['id'];
        require_once('../Database/Database.php');
        $db = new Database();
        $db->Connect();
        $shortcut = $db->SelectColumns('Series', 'Shortcut, Mulligan', 'ID_Series = ' . $id);
        if($shortcut)
        {
            $shortcut = $shortcut[0]->Shortcut;
            $mulligan = $shortcut[0]->Mulligan;

            function GetDrivers($team_id, $shortcut)
            {
                $driver_names = array();

                require_once('../Database/Database.php');
                $db = new Database();
                $db->Connect();

                $drivers = $db->SelectColumns($shortcut . '_teams', 'Drivers', 'ID_Team = ' . $team_id);
                $db->Disconnect();
                foreach($drivers as $driver)
                {
                    $driver_names[] = $driver->Name;
                }
                return $driver_names;
            }
?>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Teamwertung | Ranking - ALPHA</title>
    </head>
    <body>
        <div id="header">Teamwertung</div>
        <div id="main">
             <table>
                <tr>
                    <td>Platz</td>
                    <td>Team</td>
                    <td>Fahrer</td>
                    <?php
                        if($mulligan == 1)
                        {
                            echo '<td>Streichergebnis</td>';
                        }
                     ?>
                    <td>Punkte</td>
                </tr>
                <?php
                    $teams = $db->Select($shortcut . '_teams', '', 'Points', 'DESC');
                    $db->Disconnect();

                    if($teams > 0)
                    {
                        $count = 0;
                        foreach($teams as $team)
                        {
                            $count++;
                            echo '<tr class="team">';
                            echo '<td>' . $count . '</td>';
                            echo '<td>' . $team->Name . '</td>';
                            $drivers = GetDrivers($team->ID_Team, $shortcut);
                            echo implode(', ', $drivers);
                            if($mulligan == 1)
                            {
                                echo '<td>' . $team->Mulligan . '</td>';
                            }
                            echo '<td>' . $team->Points . '</td>';
                            echo '</tr>';
                        }
                    }
                    else
                    {
                        echo 'No data.';
                    }
                ?>
            </table>
        </div>
        <div id="footer"></div>
    </body>
</html>
<?php
        }
        else
        {
            echo 'Ung&uuml;ltige Serien ID.';
        }
    }
    else
    {
        echo 'ERROR';
    }
?>